package com.fand.platform.userCenter.controller;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.platform.userCenter.dto.DepartmentDto;
import com.fand.platform.userCenter.dto.DepartmentLaDto;
import com.fand.platform.userCenter.dto.DepartmentReDto;
import com.fand.platform.userCenter.service.UserCenterService;
import com.fand.platform.userCenter.validator.DepartmentLaValidator;
import com.fand.platform.userCenter.validator.DepartmentReValidator;
import com.fand.platform.userCenter.validator.DepartmentValidator;
import com.fand.platform.userCenter.vo.DepartmentVo;
import com.google.common.collect.Maps;

/**
 * 部门
 * 
 * @author yaoHT
 */
@Controller
public class DepartmentController {
	@Qualifier(UserCenterService.BEAN_ID)
	@Autowired(required = false)
	private UserCenterService userCenterService;

	@Qualifier(DepartmentValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator departmentValidator;

	@Qualifier(DepartmentLaValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator departmentLaValidator;

	@Qualifier(DepartmentReValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator departmentReValidator;

	@RequestMapping(value = { "/admin/userCenter/department/department/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminDepartmentDelete(final DepartmentVo departmentVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userCenterService.getDepartmentService().deleteDepartmentBy2(departmentVo.getDepartment());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/userCenter/department/department/edit.html" }, method = RequestMethod.GET)
	public String adminDepartmentEdit(final DepartmentVo departmentVo, final Model model) {
		final DepartmentDto department = this.userCenterService.getDepartmentService().getDepartmentBy2(
				departmentVo.getDepartment());

		model.addAttribute("department", department);

		return "/admin/userCenter/department/department/edit";
	}

	@RequestMapping(value = { "/admin/userCenter/department/department.html",
			"/admin/userCenter/department/department/index.html" })
	public String adminDepartmentIndex() {
		return "/admin/userCenter/department/department/index";
	}

	@RequestMapping(value = { "/admin/userCenter/department/department/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminDepartmentIndexPagination(final DepartmentVo departmentVo) {
		final PaginationDto<Map<String, Object>> departmentPagination = this.userCenterService
				.getDepartmentService().paginationDepartmentBy1ForMapList(departmentVo.getDepartment());

		final Map<String, Object> departmentPaginationMap = Maps.newHashMap();

		departmentPaginationMap.put("total", departmentPagination.getTotalSize());
		departmentPaginationMap.put("rows", departmentPagination.getDataList());

		return departmentPaginationMap;
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentLa/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminDepartmentLaDelete(final DepartmentVo departmentVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userCenterService.getDepartmentService().deleteDepartmentLaBy2(departmentVo.getDepartmentLa());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentLa/edit.html" }, method = RequestMethod.GET)
	public String adminDepartmentLaEdit(final DepartmentVo departmentVo, final Model model) {
		final DepartmentLaDto departmentLa = this.userCenterService.getDepartmentService().getDepartmentLaBy2(
				departmentVo.getDepartmentLa());

		model.addAttribute("departmentLa", departmentLa);

		return "/admin/userCenter/department/departmentLa/edit";
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentLa.html",
			"/admin/userCenter/department/departmentLa/index.html" })
	public String adminDepartmentLaIndex() {
		return "/admin/userCenter/department/departmentLa/index";
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentLa/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminDepartmentLaIndexPagination(final DepartmentVo departmentVo) {
		final PaginationDto<Map<String, Object>> departmentLaPagination = this.userCenterService
				.getDepartmentService().paginationDepartmentLaBy1ForMapList(departmentVo.getDepartmentLa());

		final Map<String, Object> departmentLaPaginationMap = Maps.newHashMap();

		departmentLaPaginationMap.put("total", departmentLaPagination.getTotalSize());
		departmentLaPaginationMap.put("rows", departmentLaPagination.getDataList());

		return departmentLaPaginationMap;
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentLa/new.html" }, method = RequestMethod.GET)
	public String adminDepartmentLaNew(final DepartmentVo departmentVo, final Model model) {
		model.addAttribute("departmentLa", departmentVo.getDepartmentLa());

		return "/admin/userCenter/department/departmentLa/new";
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentLa/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminDepartmentLaSave(final DepartmentVo departmentVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.departmentLaValidator.validate(departmentVo.getDepartmentLa(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.userCenterService.getDepartmentService().saveDepartmentLaBy1ForDepartmentLa(
					departmentVo.getDepartmentLa());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentLa/validationBy1.json" })
	public @ResponseBody
	Boolean adminDepartmentLaValidationBy1(final DepartmentVo departmentVo) {
		try {
			final int count = this.userCenterService.getDepartmentService().readDepartmentLaBy1ForInteger(
					departmentVo.getDepartmentLa());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentLa/view.html" }, method = RequestMethod.GET)
	public String adminDepartmentLaView(final DepartmentVo departmentVo, final Model model) {
		final DepartmentLaDto departmentLa = this.userCenterService.getDepartmentService().getDepartmentLaBy2(
				departmentVo.getDepartmentLa());

		model.addAttribute("departmentLa", departmentLa);

		return "/admin/userCenter/department/departmentLa/view";
	}

	@RequestMapping(value = { "/admin/userCenter/department/department/new.html" }, method = RequestMethod.GET)
	public String adminDepartmentNew(final DepartmentVo departmentVo, final Model model) {
		model.addAttribute("department", departmentVo.getDepartment());

		return "/admin/userCenter/department/department/new";
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentRe/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminDepartmentReDelete(final DepartmentVo departmentVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userCenterService.getDepartmentService().deleteDepartmentReBy2(departmentVo.getDepartmentRe());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentRe/edit.html" }, method = RequestMethod.GET)
	public String adminDepartmentReEdit(final DepartmentVo departmentVo, final Model model) {
		final DepartmentReDto departmentRe = this.userCenterService.getDepartmentService().getDepartmentReBy2(
				departmentVo.getDepartmentRe());

		model.addAttribute("departmentRe", departmentRe);

		return "/admin/userCenter/department/departmentRe/edit";
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentRe.html",
			"/admin/userCenter/department/departmentRe/index.html" })
	public String adminDepartmentReIndex() {
		return "/admin/userCenter/department/departmentRe/index";
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentRe/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminDepartmentReIndexPagination(final DepartmentVo departmentVo) {
		final PaginationDto<Map<String, Object>> departmentRePagination = this.userCenterService
				.getDepartmentService().paginationDepartmentReBy1ForMapList(departmentVo.getDepartmentRe());

		final Map<String, Object> departmentRePaginationMap = Maps.newHashMap();

		departmentRePaginationMap.put("total", departmentRePagination.getTotalSize());
		departmentRePaginationMap.put("rows", departmentRePagination.getDataList());

		return departmentRePaginationMap;
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentRe/new.html" }, method = RequestMethod.GET)
	public String adminDepartmentReNew(final DepartmentVo departmentVo, final Model model) {
		model.addAttribute("departmentRe", departmentVo.getDepartmentRe());

		return "/admin/userCenter/department/departmentRe/new";
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentRe/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminDepartmentReSave(final DepartmentVo departmentVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.departmentReValidator.validate(departmentVo.getDepartmentRe(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.userCenterService.getDepartmentService().saveDepartmentReBy1ForDepartmentRe(
					departmentVo.getDepartmentRe());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentRe/validationBy1.json" })
	public @ResponseBody
	Boolean adminDepartmentReValidationBy1(final DepartmentVo departmentVo) {
		try {
			final int count = this.userCenterService.getDepartmentService().readDepartmentReBy1ForInteger(
					departmentVo.getDepartmentRe());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/userCenter/department/departmentRe/view.html" }, method = RequestMethod.GET)
	public String adminDepartmentReView(final DepartmentVo departmentVo, final Model model) {
		final DepartmentReDto departmentRe = this.userCenterService.getDepartmentService().getDepartmentReBy2(
				departmentVo.getDepartmentRe());

		model.addAttribute("departmentRe", departmentRe);

		return "/admin/userCenter/department/departmentRe/view";
	}

	@RequestMapping(value = { "/admin/userCenter/department/department/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminDepartmentSave(final DepartmentVo departmentVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.departmentValidator.validate(departmentVo.getDepartment(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.userCenterService.getDepartmentService().saveDepartmentBy1ForDepartment(
					departmentVo.getDepartment());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/userCenter/department/department/validationBy1.json" })
	public @ResponseBody
	Boolean adminDepartmentValidationBy1(final DepartmentVo departmentVo) {
		try {
			final int count = this.userCenterService.getDepartmentService().readDepartmentBy1ForInteger(
					departmentVo.getDepartment());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/userCenter/department/department/view.html" }, method = RequestMethod.GET)
	public String adminDepartmentView(final DepartmentVo departmentVo, final Model model) {
		final DepartmentDto department = this.userCenterService.getDepartmentService().getDepartmentBy2(
				departmentVo.getDepartment());

		model.addAttribute("department", department);

		return "/admin/userCenter/department/department/view";
	}
}
